package org.jeecg.config;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 自定义 p6spy sql输出格式
 *
 * @author chechen
 * @date 2021-11-18
 */
public class P6spySqlFormatConfigure implements MessageFormattingStrategy {

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        return StringUtils.isNotBlank(sql) ? LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " | Consume Time: " + elapsed + " ms " +
                StringUtils.LF + sql.replaceAll("[\\s]+", StringUtils.SPACE) + StringPool.SEMICOLON + StringUtils.LF : StringUtils.EMPTY;
    }
}
